import {Notification} from 'element-ui';
import Router from '../router/index'
// let request = {};
// if (!process.env.IS_BASE_PACKAGE) {
let request = require("./request-base").default;
// }

request.interceptors.request.use(
  config => {
    config.timeout = config.timeout || 10000;
    // console.log("这里可以写自定义的request拦截器");
    return config;
  },
  error => {
    // Do something with request error
    // console.log(error); // for debug
    return Promise.reject(error);
  }
);

request.interceptors.response.use(
  response => {
    // console.log("这里可以写自定义的response拦截器");
    /*if(response.data.code !== 200) {
      if(response.data.code === 401) {
        Notification({
          type: "error",
          title: '错误',
          message: response.data.msg,
          showClose: false,
          duration: 200
        });
        Router.replace("/login").then(r => {
          localStorage.removeItem('user')
          localStorage.removeItem('token')
        })
      } else {
        Notification({
          type: "error",
          title: '错误',
          message: response.data.msg,
          showClose: false,
          duration: 2000
        });
      }
    }*/

    switch (response.data.code) {
      case 401: {
        Notification({
          type: "error",
          title: '错误',
          message: response.data.msg,
          showClose: false,
          duration: 1500
        });
        Router.replace("/login").then(r => {
          localStorage.removeItem('user')
          localStorage.removeItem('token')
        })
        break
      }
      case 400: {
        Notification({
          type: "error",
          title: '错误',
          message: response.data.msg,
          showClose: false,
          duration: 1500
        });
        break
      }
      case 404: {
        Notification({
          type: "error",
          title: '错误',
          message: response.data.msg,
          showClose: false,
          duration: 1500
        });
        Router.replace("/home").then(r => {
          console.log(r)
        })
        break
      }
      case 500: {
        Notification({
          type: "error",
          title: '错误',
          message: response.data.msg,
          showClose: false,
          duration: 1500
        });
        break
      }
    }
    return response
  },
  error => {
    // Do something with request error
    // console.log(error); // for debug
    return Promise.reject(error);
  }
);
export default request;
